#!/bin/sh

revision="installjava v0.3.7 (2023-06-17 MIB-Wiki)"
# use --help for more info

export PATH=:/proc/boot:/sbin:/bin:/usr/bin:/usr/sbin:/net/mmx/bin:/net/mmx/usr/bin:/net/mmx/usr/sbin:/net/mmx/sbin:/net/mmx/mnt/app/armle/bin:/net/mmx/mnt/app/armle/sbin:/net/mmx/mnt/app/armle/usr/bin:/net/mmx/mnt/app/armle/usr/sbin
export LD_LIBRARY_PATH=/net/mmx/mnt/app/root/lib-target:/net/mmx/mnt/eso/lib:/net/mmx/eso/lib:/net/mmx/mnt/app/usr/lib:/net/mmx/mnt/app/armle/lib:/net/mmx/mnt/app/armle/lib/dll:/net/mmx/mnt/app/armle/usr/lib
export IPL_CONFIG_DIR=/etc/eso/production

thisname="$(basename $0)"
thisdir="$(dirname $0)"

if [ -z $LOG ]; then
	. $thisdir/../config/GLOBALS
fi
echo -ne "\n$ME-$thisname---->\n" | $TEE -i -a $LOG

if [ -f $TMP/backup.mib ] || [ -f $TMP/reboot.mib ] || [ -f $TMP/flash.mib ]; then
	echo "Backup or reboot is already running..."
	return 2> /dev/null
fi

if [ "$2" = "-noboot" ]; then
	noboot=$2
else
	noboot=""
fi

APP="/net/mmx/mnt/app/eso/hmi/lsd/jars/"
LSD=/net/mmx/mnt/app/eso/hmi/lsd/lsd.sh
BU=/net/mmx/mnt/app/eso/hmi/lsd/lsd.sh.bu

mount -uw /net/mmx/mnt/app

if [[ "$TRAINVERSION" = *POG1* ]] || [[ "$TRAINVERSION" = *AU* ]] || [[ "$TRAINVERSION" = *BY* ]]; then
	if ! grep -q "Find and append jar files" ${LSD}; then
		if [ ! -e $BU ]; then
			echo "Backup lsd.sh" | $TEE -a $LOG
			cp -v $LSD $BU 2>> $LOG
		fi
		echo "Patching lsd.sh to run additional *.jar" | $TEE -a $LOG
		cp -r $LSD $BACKUPFOLDER/$MUVERSION-lsd.sh 2>> $LOG
		# Not working on VW/SKODA/SEAT like it is - additional jars do not load - $SED -ir 's,^$J9,## Find and append jar files\nif [[ -d /net/mmx/mnt/app/eso/hmi/lsd/jars/ ]]; then\n	JARS=$(find /net/mmx/mnt/app/eso/hmi/lsd/jars/ -name '*.zip' -or -name '*.jar')\n	if [[ ! "x$JARS" == "x" ]]; then\n		for jar in $JARS; do\n			BOOTCLASSPATH="$BOOTCLASSPATH:$jar"\n		done\n	fi\nfi\n$J9,g' $LSD 2>> $LOG
		$SED -ir 's,^$J9,BOOTCLASSPATH="$BOOTCLASSPATH -Xbootclasspath/p:$BASE_DIR/lsd/jars/NavActiveIgnore.jar"\n$J9,g' $LSD 2>> $LOG
	else
		echo "lsd.sh is already ready to load additional *.jar" | $TEE -a $LOG
	fi
elif [[ "$TRAINVERSION" = *VW* ]] || [[ "$TRAINVERSION" = *SKG1* ]] || [[ "$TRAINVERSION" = *SEG1* ]]; then
	if grep -q "Find and append jar files" ${LSD}; then
		echo -ne "Old lsd.sh patch found\n" | $TEE -a $LOG
		if [ -e $BU ]; then
			echo "Restore lsd.sh" | $TEE -a $LOG
			cp -v $BU $LSD 2>> $LOG
			echo -ne "Original lsd.sh restored\n" | $TEE -a $LOG
		else
			echo -ne "No backup found\nGet in touch with M.I.B team\n" | $TEE -a $LOG
			sleep 5
			return 2> /dev/null
		fi
		echo "Patching lsd.sh to run additional *.jar" | $TEE -a $LOG
		cp -r $LSD $BACKUPFOLDER/$MUVERSION-lsd.sh 2>> $LOG
		$SED -ir 's,^$J9,#Append jar files\nBOOTCLASSPATH="$BOOTCLASSPATH -Xbootclasspath/p:$BASE_DIR/lsd/jars/NavActiveIgnore.jar"\n$J9,g' $LSD 2>> $LOG
		echo "lsd.sh is patched" | $TEE -a $LOG
	elif ! grep -q "Append jar files" ${LSD}; then
		if [ ! -e $BU ]; then
			echo "Backup lsd.sh" | $TEE -a $LOG
			cp -v $LSD $BU 2>> $LOG
		fi
		echo "Patching lsd.sh to run additional *.jar" | $TEE -a $LOG
		cp -r $LSD $BACKUPFOLDER/$MUVERSION-lsd.sh 2>> $LOG
		# Not working on VW/SKODA/SEAT like it is - additional jars do not load - $SED -ir 's,^$J9,## Find and append jar files\nif [[ -d /net/mmx/mnt/app/eso/hmi/lsd/jars/ ]]; then\n	JARS=$(find /net/mmx/mnt/app/eso/hmi/lsd/jars/ -name '*.zip' -or -name '*.jar')\n	if [[ ! "x$JARS" == "x" ]]; then\n		for jar in $JARS; do\n			BOOTCLASSPATH="$BOOTCLASSPATH:$jar"\n		done\n	fi\nfi\n$J9,g' $LSD 2>> $LOG
		$SED -ir 's,^$J9,#Append jar files\nBOOTCLASSPATH="$BOOTCLASSPATH -Xbootclasspath/p:$BASE_DIR/lsd/jars/NavActiveIgnore.jar"\n$J9,g' $LSD 2>> $LOG
		echo "lsd.sh is patched" | $TEE -a $LOG
	else
		echo "lsd.sh is already patched" | $TEE -a $LOG
	fi
fi

echo "List .../jars/ folder content:\n" >> $LOG
ls -l $APP | $TEE -a $LOG

case $1 in

-navignoreon) {

# Thanks to andrewleech, Errivincenz, thevaan and many more for making this possible!
# https://github.com/jilleb/mib2-toolbox/pull/189
# https://github.com/Mr-MIBonk/M.I.B._More-Incredible-Bash/discussions/93

JAR="NavActiveIgnore.jar"

trap '' 2

echo -ne "-- Install NavActiveIgnore\n" | $TEE -a $LOG
if [[ "$TRAINVERSION" = MHI2_ER_VWG11_K334* ]] || [[ "$TRAINVERSION" = MHI2_ER_VWG13_K452* ]] || [[ "$TRAINVERSION" = MHI2_ER_SKG13_P452* ]] || [[ "$TRAINVERSION" = MHI2_ER_SKG11_K334* ]] || [[ "$TRAINVERSION" = MHI2_ER_SEG11_P470* ]]; then
	if [ -f $APP$JAR ]; then
		echo -ne "$JAR already present on unit.\nWill be updated with file from SD now\n" | $TEE -a $LOG
	fi
	cp -r $VOLUME/mod/java/navignore_vw.jar $APP$JAR 2>> $LOG
	echo -ne "navignore_vw.jar installed.\nUnit will reboot now\n" | $TEE -a $LOG
	[ -z "$noboot" ] && . $thisdir/reboot -t 10
elif [[ "$TRAINVERSION" = *AU57x* ]]; then
	echo -ne "AU57x FW train detected\n" | $TEE -a $LOG
	echo -ne "Due to the old internal java structure the patch is not working on any FW of this train.\n"
	echo -ne "no supported train found - will stop here\n" | $TEE -a $LOG
elif [[ "$TRAINVERSION" = *POG1* ]] || [[ "$TRAINVERSION" = *AU* ]] || [[ "$TRAINVERSION" = *BY* ]]; then
	echo -ne "Update FW to latest version is recommended.\n"
	if [ -f $APP$JAR ]; then
		echo -ne "$JAR already present on unit.\nWill be updated with file from SD now\n" | $TEE -a $LOG
	fi
	cp -r $VOLUME/mod/java/navignore_audi.jar $APP$JAR 2>> $LOG
	echo -ne "navignore_audi.jar installed.\nUnit will reboot now\n" | $TEE -a $LOG
	[ -z "$noboot" ] && . $thisdir/reboot -t 10
else
	echo -ne "Update FW to latest version to enable patch install.\n"
	echo -ne "no supported train found - will stop here\n" | $TEE -a $LOG
fi

trap 2

return 2> /dev/null

};;

-navignoreoff) {

JAR="NavActiveIgnore.jar"

trap '' 2

echo -ne "-- Remove NavActiveIgnore\n" | $TEE -a $LOG
if [ -f $APP$JAR ]; then
	rm -rf $APP$JAR 2>> $LOG
	echo "NavActiveIgnore.jar removed\nUnit will reboot now\n" | $TEE -a $LOG
	[ -z "$noboot" ] && . $thisdir/reboot -t 10
else
		echo -ne "File not found - navignore patch is not installed\nNew patch will be installed now\n" | $TEE -a $LOG
fi

trap 2

return 2> /dev/null

};;

-g24full) {

JAR="g24fullscreen.jar"

trap '' 2

echo -ne "-- Install G24 Fullscreen CarPlay patch\n" | $TEE -a $LOG
if [[ "$TRAINVERSION" = *G24* ]]; then
	echo -ne "Please note, that this patch will most likely only work on latest FW versions.\n"
	echo -ne "Update FW before applying the patch is recommended.\n"
	if [ -f $APP"g24cp.jar" ]; then
		echo -ne "File already present on unit.\nWill be updated with file from SD card now...\n" | $TEE -a $LOG
	fi
	if [ -f $APP"g24ws.jar" ]; then
		echo -ne "Old widescreen patch found\nWill be removed from unit now\nNew patch will be installed now\n" | $TEE -a $LOG
		rm $APP"g24ws.jar" 2>> $LOG
	fi
	cp -r $VOLUME/mod/java/$JAR $APP"g24cp.jar" 2>> $LOG
	echo -ne "List jar folder content:\n"
	ls -l $APP | $TEE -a $LOG
	echo -ne "$JAR installed.\nUnit will reboot now\n" | $TEE -a $LOG
	[ -z "$noboot" ] && . $thisdir/reboot -t 10
else
	echo -ne "No supported train found - will stop here\n" | $TEE -a $LOG
	echo -ne "Only Porsche/Bently G24 units are supported.\n"
fi

trap 2

return 2> /dev/null

};;

-g24wide) {

JAR="g24widescreen.jar"

trap '' 2

echo -ne "-- Install G24 Widescreen CarPlay patch\n" | $TEE -a $LOG
if [[ "$TRAINVERSION" = *G24* ]]; then
	echo -ne "Please note, that this patch will most likely only work on latest FW versions.\n"
	echo -ne "Update FW before applying the patch is recommended.\n"
	if [ -f $APP"g24cp.jar" ]; then
		echo -ne "File already present on unit.\nWill be updated with file from SD card now....\n" | $TEE -a $LOG
	fi
	if [ -f $APP"g24ws.jar" ]; then
	echo -ne "Old widescreen patch found\nWill be removed from unit now\n" | $TEE -a $LOG
	rm $APP"g24ws.jar" 2>> $LOG
	fi
	cp -r $VOLUME/mod/java/$JAR $APP"g24cp.jar" 2>> $LOG
	echo -ne "List jar folder content:\n"
	ls -l $APP | $TEE -a $LOG
	echo -ne "$JAR installed.\nUnit will reboot now\n" | $TEE -a $LOG
	[ -z "$noboot" ] && . $thisdir/reboot -t 10
else
	echo -ne "No supported train found - will stop here\n" | $TEE -a $LOG
	echo -ne "Only Porsche/Bently G24 units are supported.\n"
fi

trap 2

return 2> /dev/null

};;

-g24rm) {

trap '' 2

echo -ne "-- Remove G24 CarPlay patch\n" | $TEE -a $LOG
if [[ "$TRAINVERSION" = *G24* ]]; then
	if [ -f $APP"g24cp.jar" ]; then
		rm -f $APP"g24cp.jar" 2>> $LOG
		echo "G24 patch removed from unit.\nUnit will reboot now\n" | $TEE -a $LOG
		[ -z "$noboot" ] && . $thisdir/reboot -t 10
	else
		echo -ne "No patch found on unit\n" | $TEE -a $LOG
	fi
else
	echo -ne "No supported train found - will stop here\n" | $TEE -a $LOG
	echo -ne "Only Porsche/bently G24 units are supported.\n"
fi

trap 2

return 2> /dev/null

};;

# help or unknown parameter ------------------------------
	*) {
		echo ""
		echo $revision
		echo ""
		echo "Usage: "$thisname" [OPTION] [-noboot]"
		echo ""
		echo "Options:"
		echo "        -navignoreon 		enable navignore"
		echo "        -navignoreoff 		remove navignore"
		echo "        -g24full 		install G24 fullscreen patch"
		echo "        -g24wide 		install G24 widescreen patch"
		echo "        -g24rm 			remove G24 patch"
		echo "        --help			show this help"
		echo ""
		echo "Note: Backup will only work in RCC bash!"
		echo ""
		echo "This program is free software; you can redistribute it and/or"
		echo "modify it under the terms of the GNU General Public License"
		echo "as published by the Free Software Foundation; either version 2"
		echo "of the License, or (at your option) any later version."
		echo ""
		echo "This program is distributed in the hope that it will be useful,"
		echo "but WITHOUT ANY WARRANTY; without even the implied warranty of"
		echo "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."
		echo "See the GNU General Public License for more details."
		echo ""
		echo "You should have received a copy of the GNU General Public License"
		echo "along with this program; if not, write to the Free Software Foundation,"
		echo "Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA."
		echo ""
		
	};;
	
	esac

exit 0

